<?php
//$_GET['id']
//
//$_POST['id']
//$_POST['level']
//$_POST['question']
//$_POST['answer']

	include 'include/function.php';

	if(!isset($_user_id))redirect_error(_d('err_need_login'));
	
	if(isset($_POST['level'])){
		$addquestion_id			=get_to_mysql($_POST['id']);
		$addquestion_level		=get_to_mysql($_POST['level']);
		$addquestion_question	=get_to_mysql($_POST['question']);
		$addquestion_answer		=get_to_mysql($_POST['answer']);
		//权限限制
		$addquestion_perm=permission_check_add($_user_id,$addquestion_id);
		if(!strstr($addquestion_perm[0],$addquestion_level))
			redirect_error("You can't add the question of $addquestion_level, because you havn't it.");
		//执行
		$query='insert into question (node_id, level, question, answer, edit_time) '
			. "values ($addquestion_id, '$addquestion_level', '$addquestion_question', '$addquestion_answer', now()) ";
		$ret=oj_query($query);
		
		redirect("showquestion.php?id=$addquestion_id");
	}
	$addquestion_id=-1;
	if(isset($_GET['id']))
		$addquestion_id=get_to_html($_GET['id']);
	$addquestion_perm=permission_check_add($_user_id,$addquestion_id);
	
function draw_fillin($left,$input,$name,$prop='',$right='',$value=''){
	echo '<tr>';
	echo '<td>' . str_replace("\n",'<br>',htmlspecialchars($left)) . '</td>';
	echo "<td><$input name='$name' $prop>" . htmlspecialchars($value) . "</$input>$right</td>";
	echo '</tr>';
}

	include 'include/header.php';
	echo "<title>$site_title - Add Question</title>";
?>

<form method=post action="addquestion.php">
<?php echo "<input type='hidden' name='id' value='$addquestion_id'>"; ?>
<table align=center>
	<tr>
		<td>Node ID:</td>
		<td><?php echo "<a href='shownode.php?id=$addquestion_id'>$addquestion_id</a>"; ?></td>
	</tr>
	<tr>
		<td>Level:</td>
		<td>
			<?php
				echo '<select name="level">';
				foreach($level_array as $key=>$val){
					if(strstr($addquestion_perm[0],$key)){
						echo "<option value='$key'";
						if('view'==$key&&strlen('view')==strlen($key))echo ' selected';
						echo ">$val</option>";
					}
				}
				echo '</select>';
			?>
		</td>
	</tr>
<?php
	draw_fillin('Question(html):','textarea','question');
	draw_fillin("Answer\n(please enter the acceptable answers \nusing this format: 'a','b','c'..., \nthe answers are case insensitive):",'textarea','answer');
?>
	<tr>
		<td></td>
		<td><input type='submit' name='submit' value='Submit'/></td>
	</tr>
</table>
</form>

<?php
	include 'include/footer.php';
?>

